using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WebMenuConfig
{
    /* This program reads a text file 
     * writes out a binary file 
     * checks that the binary file is written correctly.
     */ 
    class Config
    {
        private string sDBname;
        private string sDBuser;
        private string sDBpass;
        private string sDBIPaddress;
        private const string ASCconfigFile = @"myWebMenuConfig.txt";  // in text format - kept in local PC
        private const string BINconfigFile = @"WebMenuConfig";    // in binary format - kept in server

        static void Main(string[] args)
        {
            Config aProg = new Config();
            aProg.readFromText();
            aProg.writeToBinary();
            // Test whether successful?
            if(aProg.checkConfig()) {
                Console.WriteLine("Configuration created successfully.");
            }else{
                Console.WriteLine("Configuration NOT successful.");
            }
            Console.ReadLine();
        }

        private void readFromText(){
            TextReader tr = new StreamReader(ASCconfigFile);
            sDBname = tr.ReadLine();
            sDBuser = tr.ReadLine();
            sDBpass = tr.ReadLine();
            sDBIPaddress = tr.ReadLine();
            tr.Close();

            // Encode to Base64
            sDBname = this.EncodeTo64(sDBname);
            sDBuser = this.EncodeTo64(sDBuser);
            sDBpass = this.EncodeTo64(sDBpass);
            sDBIPaddress = this.EncodeTo64(sDBIPaddress);
        }

        private void writeToBinary(){
            BinaryWriter bw = new BinaryWriter(new FileStream(BINconfigFile, FileMode.Create));
            bw.Write(sDBname);
            bw.Write(sDBuser);
            bw.Write(sDBpass);
            bw.Write(sDBIPaddress);
            bw.Close();
        }

        /* for Testing
         */
        private bool checkConfig()
        {
            bool bCheck;
            // Reading the Binary File
            BinaryReader br = new BinaryReader(new FileStream(BINconfigFile, FileMode.Open));
            string sDBnameTmp = br.ReadString();
            string sDBuserTmp = br.ReadString();
            string sDBpassTmp = br.ReadString();
            string sDBIPaddressTmp = br.ReadString();
            br.Close();
            if (this.sDBname.Equals(sDBnameTmp) && this.sDBuser.Equals(sDBuserTmp) &&
                this.sDBpass.Equals(sDBpassTmp) && this.sDBIPaddress.Equals(sDBIPaddressTmp))
            {
                bCheck = true;
            }
            else { 
                bCheck = false; 
            }

            //Example of Decoding back from Base64
            sDBnameTmp = this.DecodeFrom64(sDBnameTmp);

            
            return bCheck;
        }

        private string EncodeTo64(string toEncode)
        {
            byte[] toEncodeAsBytes
                  = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
            string returnValue
                  = System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }

        private string DecodeFrom64(string encodedData)
        {
            byte[] encodedDataAsBytes
                = System.Convert.FromBase64String(encodedData);
            string returnValue =
               System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
            return returnValue;
        }

    }
}
